package com.aidex.biz.utils;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

import cn.hutool.crypto.SecureUtil;
import org.apache.commons.codec.binary.Base64;
import javax.crypto.spec.IvParameterSpec;
import java.nio.charset.StandardCharsets;
/**
 * 放鑫钱包 加密
 */
public class FangXinQianBaoAESUtils {
    public static final String AES_CBC_PKCS5PADDING = "AES/CBC/PKCS5PADDING";
    public static final String KEY = "lH6FV96Y9J7Iiof2";


    /**
     * key为16位
     *
     * @param key * @param value * @return
     */
    public static String encrypt(String key, String value) {
        return encrypt(key, key, value);
    }

    public static String encrypt(String key, String initVector, String value) {
        try {
            IvParameterSpec iv = new IvParameterSpec(initVector.getBytes(StandardCharsets.UTF_8));
            SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), "AES");
            Cipher cipher = Cipher.getInstance(AES_CBC_PKCS5PADDING);
            cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
            byte[] encrypted = cipher.doFinal(value.getBytes());
            return Base64.encodeBase64String(encrypted);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return null;
    }

    /**
     * @param key
     * @param encrypted
     * @return
     */
    public static String decrypt(String key, String encrypted) {
        return decrypt(key, key, encrypted);
    }


    public static String decrypt(String key, String initVector, String encrypted) {
        try {
            IvParameterSpec iv = new IvParameterSpec(initVector.getBytes(StandardCharsets.UTF_8));
            SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), "AES");
            Cipher cipher = Cipher.getInstance(AES_CBC_PKCS5PADDING);
            cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
            byte[] original = cipher.doFinal(Base64.decodeBase64(encrypted));
            return new String(original);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }




    public static void main1(String[] args) {

        String key = "9GI8m1HAU%3ZTRCU";
        String encryptData1 = "xWeYEOICxYjtrkk5VXp0kTMXyFRzajgPUHJ7slb7YHs=";
        String encryptData2 = "UJ+k275DZD9FOKLpsKJ2l0zIYJVtMRtN67jbuwBQXzzFCpoQ0RVLYuekSq5m+2G74OwFxK7FdUkW9be8nvfR8VP5NMBtg5tbNB1nqVxfWfvw4ugOW7LIDeXziqVi+b0Um7mEq/giQlWc1qORpoTVO9MaL8ukVDzXc2ClsApnfL8aOKYOccGJR2UIm75JOw0twqS6xIJccA2qF7hlbhKt4QryAxd/Dl0ipWbNXraOuI+qS5kHxyHyDaKt6LvBmKjWXxr0BuU3AKQ7NPblmkkQ0VONuaFTm76l17B9nY4fqZfx4PU+/ip+iTsfVNjGYkt3lp+/311eiyurbCjkYY3HTAJ6+Av+39cRrpjuJ2b4MFoeodbEFiNuiucyZAXL+WRynQfgqQ+k5BgQ/oyU5XJlmQei5r7E1Z/bTx2mu03kxSaOqFXrTwrFqs+M6l3UdgDLt8P+0srYu0fw01MRf/km87PqbEvhvLUqTlGZxqpQTHPlVDuxC2sbNBMjVUC6N93ns9MfiNQm3HqsHvRm2XAkEsvGyycej36HqlvbuQjfFv0xMMWAQHhJ7rr18W3FW7XT9MEyPqKmHrZtt6ia80PKg1rJPjV2JXfrsntukMhR4Tw=";
        String iv = key;
        String decryptData1 = FangXinQianBaoAESUtils.decrypt(key, iv, encryptData1);
        String decryptData2 = FangXinQianBaoAESUtils.decrypt(key, iv, encryptData2);

        System.out.println(decryptData1);
        System.out.println(decryptData2);

        System.out.println(encryptData1.equals(FangXinQianBaoAESUtils.encrypt(key, iv, decryptData1)));
        System.out.println(encryptData2.equals(FangXinQianBaoAESUtils.encrypt(key, iv, decryptData2)));
    }




    public static void main(String[] args) {


    }
}